package chen.huai.jie.spring.xmlconfig.service.impl;

import chen.huai.jie.spring.xmlconfig.annotation.HibernateTransactional;
import chen.huai.jie.spring.xmlconfig.annotation.JpaTransactional;
import chen.huai.jie.spring.xmlconfig.db.hibernate.entity.Teacher;
import chen.huai.jie.spring.xmlconfig.db.hibernate.repository.TeacherRepository;
import chen.huai.jie.spring.xmlconfig.db.jpa.entity.Student;
import chen.huai.jie.spring.xmlconfig.db.jpa.repository.JpaStudnetRepository;
import chen.huai.jie.spring.xmlconfig.db.mybatis.entity.User;
import chen.huai.jie.spring.xmlconfig.db.mybatis.repository.UserRepository;
import chen.huai.jie.spring.xmlconfig.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private TeacherRepository teacherRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private JpaStudnetRepository jpaStudnetRepository;

    @Async
    public void asyncMethod() {
        logger.info("【asyncMethod】方法的执行线程:" + Thread.currentThread().getName());
    }

    @Override
    @HibernateTransactional
    public void addTeacher() {
        Teacher teacher = new Teacher();
        teacher.setId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        teacher.setName("chenhuaijie");
        teacherRepository.save(teacher);
    }

    @Override
    @Transactional
    public void addUser() {
        User user = new User();
        user.setId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        user.setName("chenhuaijie");
        userRepository.insert(user);
    }

    @Override
    @JpaTransactional
    public void addStudent() {
        Student student = new Student();
        student.setId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        student.setName("chenhuaijie");
        jpaStudnetRepository.save(student);
    }
}
